/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import datamodel.Notification;
import datastorage.NotificationStorage;
import java.util.Hashtable;

/**
 *
 * @author Kvest
 */
public class NotificationManager 
{
    public static final int NEW_ORDER = 0;
    public static final int NEW_MESSAGE = 1;
    public static final int ORDER_STATE_CHANGED = 2;
    public static final int ORDER_CANCELED = 3;
    public static final int CONNECTION_ERROR = 4;
    public static final int NEW_EFIR_ORDER = 5;
    public static final int FIRST_IN_QUEUE = 6;
    
    private static NotificationManager manager;
    private static final Object lockObject = new Object();
    
    public static NotificationManager getNotificationManager()
    {
        synchronized(lockObject)
        {
            if (manager == null)
            {
                manager = new NotificationManager();
            }
        }
        
        return manager;
    }
    
    private NotificationStorage storage;
    
    private final Hashtable notifications;
    
    private NotificationManager()
    {
        notifications = new Hashtable(6);
        
        //Создаем дефолтные оповещения 
        createNotifications();
        
        //Создаем хранилище
        storage = new NotificationStorage();
        
        //Грузим оповещения
        load();
    }
    
    public final void load()
    {
        synchronized(notifications)
        {
            storage.load(notifications);
        }
    }
    
    public void save()
    {
        synchronized(notifications)
        {
            storage.save(notifications);
        }
    }
    
    private void createNotifications()
    {
        synchronized(notifications)
        {
            notifications.put(new Integer(NEW_ORDER), new Notification(NEW_ORDER));
            notifications.put(new Integer(NEW_MESSAGE), new Notification(NEW_MESSAGE));
            notifications.put(new Integer(ORDER_STATE_CHANGED), new Notification(ORDER_STATE_CHANGED));
            notifications.put(new Integer(ORDER_CANCELED), new Notification(ORDER_CANCELED));
            notifications.put(new Integer(CONNECTION_ERROR), new Notification(CONNECTION_ERROR));
            notifications.put(new Integer(NEW_EFIR_ORDER), new Notification(NEW_EFIR_ORDER));
            notifications.put(new Integer(FIRST_IN_QUEUE), new Notification(FIRST_IN_QUEUE));
        }
    }
    
    public Notification getNotification(int eventType)
    {
        synchronized(notifications)
        {
            return (Notification)notifications.get(new Integer(eventType));
        }
    }
    
    public void event(int eventType)
    {
        Notification notification;
        synchronized(notifications)
        {
            notification = (Notification)notifications.get(new Integer(eventType)); 
        }
        
        if (notification != null)
        {
            notification.start();
        }
    }
}
